package com.link2room.quintesson.facade.impl;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.jpa.result.QueryResult;

import com.link2room.enums.exception.QuintessonSysExceptionEnum;
import com.link2room.jetfire.constant.ReservationConstant;
import com.link2room.jetfire.dto.param.base.ReservationParamDto;
import com.link2room.jetfire.dto.param.inside.ReservationPayInsideParamDto;

import com.link2room.jetfire.dto.result.base.ReservationAccountDto;
import com.link2room.jetfire.dto.result.base.ReservationBaseDto;
import com.link2room.jetfire.dto.result.base.ReservationDto;
import com.link2room.quintesson.dto.param.manager.TaobaoAccountsManagerParamDto;
import com.link2room.quintesson.exception.TaobaoException;
import com.link2room.quintesson.facade.ITaobaoAccountsFacadeService;
import com.link2room.quintesson.feign.jetfire.ReservationsFeignService;
import com.link2room.quintesson.feign.soundwave.PmsReservationFeignService;
import com.link2room.quintesson.service.ITaobaoAPIService;
import com.link2room.soundwave.dto.param.base.PmsReservationBaseParamDto;
import com.link2room.soundwave.dto.result.base.AccountResultDto;
import com.link2room.soundwave.dto.result.base.PmsAccountDto;
import com.link2room.trailbreaker.constant.Pay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * Created by sss on 2017/5/3.
 */
@Component("taobaoAccountsFacadeService")
public class TaobaoAccountsFacadeServiceImpl implements ITaobaoAccountsFacadeService {

    @Autowired
    private PmsReservationFeignService pmsReservationFeignService;
    @Autowired
    private ReservationsFeignService reservationsFeignService;
    @Resource
    private ITaobaoAPIService taobaoAPIService;

    /**
     * 获取PMS账单
     * @param taobaoAccountsManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public QueryResult<PmsAccountDto> listAccounts(TaobaoAccountsManagerParamDto taobaoAccountsManagerParamDto)throws Exception{
        PmsReservationBaseParamDto pmsReservationBaseParamDto=new PmsReservationBaseParamDto();
        pmsReservationBaseParamDto.setLhotelCode(taobaoAccountsManagerParamDto.getLhotelCode());
        pmsReservationBaseParamDto.setL2rNo(taobaoAccountsManagerParamDto.getL2rNo());
        pmsReservationBaseParamDto.setCrsNo(taobaoAccountsManagerParamDto.getCrsNo());
        return pmsReservationFeignService.getPMSReservationsAccounts(pmsReservationBaseParamDto.paramToMap()).getQueryResult();
    }

    /**
     * 计算账务
     * @param taobaoAccountsManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public AccountResultDto getAccountResult(TaobaoAccountsManagerParamDto taobaoAccountsManagerParamDto) throws Exception{
        PmsReservationBaseParamDto pmsReservationBaseParamDto=new PmsReservationBaseParamDto();
        pmsReservationBaseParamDto.setIds(taobaoAccountsManagerParamDto.getIds());
        return pmsReservationFeignService.pmsAccountsCount(pmsReservationBaseParamDto.paramToMap()).getResultInfo();
    }

    @Override
    public void taobaoAccountSettle(TaobaoAccountsManagerParamDto taobaoAccountsManagerParamDto)throws Exception{
        ReservationParamDto reservationInsideParamDto=new ReservationParamDto();
        reservationInsideParamDto.setL2rNo(taobaoAccountsManagerParamDto.getL2rNo());
        ReservationDto reservationDto = reservationsFeignService.getReservation(reservationInsideParamDto.paramToMap()).getResultInfo();
        ReservationBaseDto reservationBase=reservationDto.getReservationBaseDto();
        ReservationAccountDto reservationAccountDto=reservationDto.getReservationAccountDto();
        AccountResultDto accountResult=this.getAccountResult(taobaoAccountsManagerParamDto);
        BigDecimal roomFee=accountResult.getPayableRoomCharge();
        BigDecimal otherFee= accountResult.getPayableOteherCharge();
        if(roomFee.compareTo(BigDecimal.ZERO)<=0||otherFee.compareTo(BigDecimal.ZERO)==-1 ){
            throw new TaobaoException(QuintessonSysExceptionEnum.TAOBAO_SETTLE_MONEY_ERROR,"房费："+roomFee.toString()+"杂费："+otherFee.toString());
        }

        if( reservationAccountDto.getL2rPaysta().equals(ReservationConstant.PMSPAYSTA.NONE)
                ||reservationAccountDto.getL2rPaysta().equals(ReservationConstant.PMSPAYSTA.REJECT) ){
            //发起信用住
            taobaoAPIService.alipayfaceSettle(taobaoAccountsManagerParamDto.getLhotelGroupCode(),
                    reservationBase.getTerminalNo(),
                    reservationBase.getL2RNo(),
                    reservationBase.getDep(),
                    roomFee,
                    otherFee);

            //发起之后往入账
            ReservationPayInsideParamDto payParamDto = new ReservationPayInsideParamDto();
            payParamDto.setLhotelGroupCode(reservationBase.getLhotelGroupCode());
            payParamDto.setLhotelCode(reservationBase.getLhotelCode());
            payParamDto.setTaNo(reservationBase.getTerminalNo());
            payParamDto.setPayChannel(Pay._Channel.ALIPAY);
            payParamDto.setPayType(Pay._Type.ALIPAY.ALI_ORDER_PRE_PAY);
            payParamDto.setAmount(roomFee.add(otherFee));
            payParamDto.setOrderNo(reservationBase.getL2RNo());
            payParamDto.setRemark("飞猪信用住："+payParamDto.getAmount());
            payParamDto.setLimitOnce(DefaultValue.F);
            reservationsFeignService.payment(payParamDto);
        }
    }
}
